package org.yczbj.ycvideoplayer.ability;

import ohos.aafwk.content.Intent;
import ohos.agp.components.StackLayout;

import com.yc.video.player.VideoPlayer;
import com.yc.video.player.VideoViewManager;
import com.yc.video.tool.PlayerUtils;

import org.yczbj.ycvideoplayer.ResourceTable;

/**
 * DetailAbility
 *
 * @since 2021-05-12
 */
public class DetailAbility extends BaseAbility {
    /**
     * 播放容器
     */
    public static final String VIEW_NAME_PLAYER_CONTAINER = "player_container";
    private StackLayout mPlayerContainer;
    private VideoPlayer mVideoView;

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_detail_video;
    }

    @Override
    protected void initComponent(Intent intent) {
        super.initComponent(intent);
        initView();
        initVideoView();
    }

    /**
     * 初始化视图
     */
    protected void initView() {
        mPlayerContainer = (StackLayout) findComponentById(ResourceTable.Id_player_container);
        mPlayerContainer.setName(VIEW_NAME_PLAYER_CONTAINER);
        initVideoView();
    }

    private void initVideoView() {
        // 拿到VideoView实例
        VideoPlayer mVideoView = VideoViewManager.instance().get("seamless");

        // 如果已经添加到某个父容器，就将其移除
        PlayerUtils.removeViewFormParent(mVideoView);

        // 把播放器添加到页面的容器中
        mPlayerContainer.addComponent(mVideoView);

        // 设置新的控制器
        Intent intent = getIntent();
        boolean seamlessPlay = intent.getBooleanParam(IntentKeys.SEAMLESS_PLAY, false);
        if (!seamlessPlay) {
            // 不是无缝播放的情况
            String url = intent.getStringParam(IntentKeys.URL);
            mVideoView.setUrl(url);
            mVideoView.start();
        }
    }

    @Override
    protected void onInactive() {
        if (isTerminating()) {
            // 移除Controller
            mVideoView.setController(null);
            // 将VideoView置空，其目的是不执行 super.onPause(); 和 super.onDestroy(); 中的代码
            mVideoView = null;
        }
        super.onInactive();
    }
}
